package com.ctyon.watchmanager.ui.fragment;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CompoundButton;

import com.ctyon.common.util.DataUtil;
import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.bean.StepBean;
import com.ctyon.watchmanager.databinding.FragmentStepBinding;
import com.ctyon.watchmanager.dialog.PickerDialog;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.present.StepPresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * CreateDate：19-6-25 on 上午9:14
 * Describe:
 * Coder: lee
 */
public class StepFragment extends BaseFragment {

    public static final String TAG = "StepFragment";

    private FragmentStepBinding mBinding;
    private PickerDialog dialog;
    private final String[] stepArr = new String[3];
    private int stepIndex;
    private String steps;
    private StepBean stepBean;
    private boolean mStepConfig;
    private long stepCount;

    public static StepFragment newInstance() {
        final StepFragment fragment = new StepFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_step, container, false);
//        mBinding.setStepPresenter(stepPresenter);

//        mStepConfig = BasicApp.getInstance().getWatch().getStepConfig() == 1;
//        mBinding.setConfig(mStepConfig);

        stepCount = BasicApp.getInstance().getWatch().getStep();
        mBinding.setStep(String.valueOf(stepCount));
        return mBinding.getRoot();

    }

//    @Override
//    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
//        super.onActivityCreated(savedInstanceState);
//        steps = BasicApp.getInstance().getWatch().getStepIntervals();
//        stepBean = new StepBean();
//        if (!DataUtil.isEmptyAnd(steps)){
//            String[] strings = steps.split("&");
//            for (int i = 0; i < strings.length; i++) {
//                stepArr[i] = strings[i];
//            }
//            mBinding.setStepBean(stepBean.setStep(stepArr));
//        }
//    }
//
//    private final StepPresenter stepPresenter = new StepPresenter() {
//        @Override
//        public void onSaveBtClick() {
//            StringBuilder builder = new StringBuilder();
//            for (int i = 0; i < stepArr.length; i++) {
//                if(DataUtil.isEmptyOr(stepArr[i])){
//                    continue;
//                }
//                if (builder.length() > 0){
//                    builder.append("&");
//                }
//                builder.append(stepArr[i]);
//            }
//            String data = builder.toString();
//            Logger.t(LTAG).d("stepData : " + data);
//            if (!DataUtil.isEmptyAnd(BasicApp.getInstance().getWatch().getStepIntervals()) && data.contains(BasicApp.getInstance().getWatch().getStepIntervals())){
//                TSnackbarUtil.TSBError(getActivity(), "无修改");
//                return;
//            }
//            final String method = "noStep";
//            if (NetService.getInstance().isDisable(method)){
//                TSnackbarUtil.TSBError(getActivity(), "请稍候...");
//                return;
//            }
//            NetService.getInstance()
//                    .method(method)
//                    .stepIntervals(BasicApp.getInstance().getManager().getManagerId(), BasicApp.getInstance().getWatch().getMeid(), data)
//                    .timeout(10, TimeUnit.SECONDS)
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe(new BaseObserver<BaseResponseEntity>() {
//                        @Override
//                        public void onSuccess(BaseResponseEntity responseEntity) {
//                            Logger.d("onSuccess : " + responseEntity.toString());
//                            TSnackbarUtil.TSBSuccess(getActivity(), "操作成功");
//                            BasicApp.getInstance().getWatch().setStepIntervals(data);
//                        }
//
//                        @Override
//                        public String onOver() {
//                            return method;
//                        }
//                    });
//        }
//
//        @Override
//        public void onTimeClick(int index) {
//            Logger.t(LTAG).i("onTimeClick : " + index);
//            stepIndex = index;
//            showDialog();
//        }
//
//        @Override
//        public void onCheckedChanged(CompoundButton button, boolean checked) {
//            if (mStepConfig == checked) {
//                return;
//            }
//            NetService.getInstance()
//                    .stepConfig(BasicApp.getInstance().getManager().getManagerId(),
//                            BasicApp.getInstance().getWatch().getMeid(),
//                            checked)
//                    .timeout(10, TimeUnit.SECONDS)
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe(new BaseObserver<BaseResponseEntity>() {
//                        @Override
//                        public void onSuccess(BaseResponseEntity responseEntity) {
//                            Logger.d("onSuccess : " + responseEntity.toString());
//                            TSnackbarUtil.TSBSuccess(getActivity(), checked ? "开启成功" : "关闭成功");
//                            mStepConfig = checked;
//                            mBinding.setConfig(mStepConfig);
//                            BasicApp.getInstance().getWatch().setStepConfig((byte) (mStepConfig?1:0));
//                        }
//
//                        @Override
//                        public void onErrorE(Throwable e) {
//                            TSnackbarUtil.TSBSuccess(getActivity(), checked ? "开启失败" : "关闭失败");
//                            mStepConfig = !checked;
//                        }
//                    });
//        }
//    };
//
//    private final PickerDialog.OnPickerCallback dialogClickListener = (hourB, minuteB, hourE, minuteE) -> {
//        StringBuilder stepBuilder = new StringBuilder();
//        stepBuilder.append(hourB);
//        stepBuilder.append("*");
//        stepBuilder.append(minuteB);
//        stepBuilder.append("-");
//        stepBuilder.append(hourE);
//        stepBuilder.append("*");
//        stepBuilder.append(minuteE);
//        String time = stepBuilder.toString();
//        Logger.t(LTAG).i("onTimeData : " + time);
//        stepArr[stepIndex] = time;
//        mBinding.setStepBean(stepBean.setStep(stepArr));
//    };
//
//    private void showDialog() {
//        if (null == dialog){
//            Logger.t(LTAG).i("Create Dialog");
//            dialog = new PickerDialog(getActivity(), R.style.CustomDialog);
//            dialog.setListener(dialogClickListener);
//        }
//        Window window = dialog.getWindow();
//        window.setGravity(Gravity.CENTER);
//        window.setWindowAnimations(R.style.CustomDialogStyle);
//        dialog.show();
//    }
}
